﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Threading;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using BrainTechLLC.FileMaintenance;
using BrainTechLLC.WPF;
using DragDropLibrary;

using System.ComponentModel;
using BrainTechLLC.ThreadSafeObjects;
using System.Diagnostics;

namespace BrainTechLLC.FolderExplorer
{
    public partial class FolderExplorerControl : UserControl
    {
        private void mnuNewDirectory_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            FolderViewModel folder = mi.Tag as FolderViewModel;
            OperationHandler.CreateNewDirectory(folder, DirectoriesToExamineViewModel);
        }

        public void PutFolderInEditMode(FolderViewModel folder)
        {
            folder.IsEditMode = true;
            object found = folder._uiContainer.FindName("txt");
            if (found != null)
            {
                TextBox tb = found as TextBox;
                tb.Focus();
                tb.Select(tb.Text.Length, 0);
            }
        }

        private void mnuRename_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            FolderViewModel folder = mi.Tag as FolderViewModel;
            RenameFolder(folder);
        }

        public void RenameFolder(FolderViewModel folder)
        {
            PutFolderInEditMode(folder);
        }

        private void txt_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            FolderViewModel f = tb.Tag as FolderViewModel;
            f.IsEditMode = false;
        }

        private void txtBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock tblock = sender as TextBlock;
            _lastFolder = tblock.Tag as FolderViewModel;
            var pos = e.MouseDevice.GetPosition(this);
            _lastPos = pos;
            DispatcherTimer tmr = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 1, 200), IsEnabled = true };
            tmr.Tick += new EventHandler(tmr_Tick);
            tmr.Start();
        }

        private void mnuShowDirSizes_Click(object sender, RoutedEventArgs e)
        {
            if (NoSizes)
            {
                return;
            }

            MenuItem mi = sender as MenuItem;
            FolderViewModel folder = mi.Tag as FolderViewModel;
            List<FolderViewModel> folders = GetSelectedFolders();
            _root.AddOperation("ShowDirSizes");
            ParameterizedThreadStart ts = new ParameterizedThreadStart(CalcDirSize);
            Thread t = new Thread(ts) { IsBackground = true, Priority = ThreadPriority.BelowNormal };
            t.Start(folders);
        }

        public void CalcDirSize(object param)
        {
            if (NoSizes)
            {
                return;
            }
            List<FolderViewModel> folder = param as List<FolderViewModel>;
            for (int n = 0; n < folder.Count; n++)
            {
                if (!folder[n]._CorrectDirectorySize)
                {
                    folder[n].CalcDirSize();
                    folder[n].ClearFiles();
                }
            }
            _root.RemoveOperation("ShowDirSizes");
        }

        private void mnuFindFoldersToDelete_Click(object sender, RoutedEventArgs e)
        {
            FolderViewModel f = (sender as MenuItem).Tag as FolderViewModel;
            OperationHandler.FindFoldersToDeleteAsync(f);
        }

        private void mnuDeleteSelected_Click(object sender, RoutedEventArgs e)
        {
            FolderViewModel f = (sender as MenuItem).Tag as FolderViewModel;

            var result = MessageBox.Show(string.Format("Delete all checked folders under {0}?", f.FullPath), "Delete?", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                OperationHandler.DeleteCheckedUnderFolderBackground(f);
            }
        }

        private void mnuClearSelection_Click(object sender, RoutedEventArgs e)
        {
            RootFolder.GetAllFoldersInMemory().ForEach(f => f.IsSelected = false);
        }

        private void mnuExpandChildren_Click(object sender, RoutedEventArgs e)
        {
            FolderViewModel f = (sender as MenuItem).Tag as FolderViewModel;

            var folders = GetSelectedFolders();
            bool found = false;

            for (int n = 0; n < folders.Count; n++)
            {
                FolderViewModel folder = folders[n];
                if (folder == f)
                {
                    found = true;
                }
                folder.ExpandAllChildrenAsync();
            }

            if (!found)
            {
                f.ExpandAllChildrenAsync();
            }
        }

        private void chk_Click(object sender, RoutedEventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            FolderViewModel folder = chk.Tag as FolderViewModel;
            TreeViewItem tvi = chk.FindParent<TreeViewItem>();
            int idx = FindIndex(tvi);            
            GetSelectedFolders().ForEach(f => f.IsChecked = !folder.IsChecked);
        }

        private void mnuSelectAll_Click(object sender, RoutedEventArgs e)
        {
            RootFolder.GetAllFoldersInMemory().ForEach(f => f.IsSelected = true);
        }

        private void mnuOpenFolder_Click(object sender, RoutedEventArgs e)
        {
            FolderViewModel folder = (sender as MenuItem).Tag as FolderViewModel;

            ProcessStartInfo psi = new ProcessStartInfo()
            {
                UseShellExecute = true,
                WorkingDirectory = folder.FullPath,
                FileName = folder.FullPath,
                WindowStyle = ProcessWindowStyle.Normal,
            };
            Process p = new Process()
            {
                StartInfo = psi,
            };
            p.Start();
        }

        private void mnuCollapseAll_Click(object sender, RoutedEventArgs e)
        {
            FolderViewModel f = (sender as MenuItem).Tag as FolderViewModel;

            var folders = GetSelectedFolders();
            bool found = false;

            for (int n = 0; n < folders.Count; n++)
            {
                FolderViewModel folder = folders[n];
                if (folder == f)
                {
                    found = true;
                }
                folder.IsExpanded = false;
            }

            if (!found)
            {
                f.IsExpanded = false;
            }
        }
    }
}
